<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Model;
use App\Models\Asset;
use App\Models\AssetStock;
use App\Models\Customer;
use App\Models\FilterableAssetBundle\FilterableAssetBundle;
use App\Models\Order\Order;
use App\Models\Order\OrderBundle;
use App\Models\Order\OrderBundleItem;
use App\Models\Order\OrderExtraFee;
use App\Models\Order\OrderItem;
use App\Models\Order\OrderStatus;
use App\Models\Order\PaymentMethod;
use App\Models\User;
use Faker\Generator as Faker;
use Lrental\Utils\OrderNumberGenerator;


$factory->define(OrderExtraFee::class, function (Faker $faker) {
    return [
        "name" => $faker->name,
        "quantity" => $faker->randomNumber(1),
        "unit_price" => $faker->randomFloat(2, 100, 1000),
    ];
});

$factory->define(OrderItem::class, function (Faker $faker) {
    $clazz = $faker->randomElement([Asset::class, AssetStock::class, OrderExtraFee::class]);
    $item = factory($clazz)->create();
    $unitPrice = $faker->randomFloat(2, 10, 100);
    $quantity = 1;
    if ($clazz == AssetStock::class) {
        $quantity = $faker->randomNumber(1);
    }
    return [
        "order_id" => 1,
        "name" => $item->name,
        "rentalable_id" => $item->id,
        "rentalable_type" => $clazz,
        "unit_price" => $unitPrice,
        "quantity" => $quantity,
        "amount" => $unitPrice * $quantity,
        "rental_out_at" => null,
        "return_at" => null
    ];
});

/**
 * 创建的时候是要传入 order_bundle_id 字段
 */
$factory->define(OrderBundleItem::class, function (Faker $faker) {
    // need order_bundle_id
    $asset = factory(Asset::class)->create();
    return [
        "order_bundle_id" => 1,
        "name" => $faker->name,
        "rentalable_type" => Asset::class,
        "rentalable_id" => $asset->id,
        "quantity" => 1,
        "unit_price" => 1,
        "amount" => 0,
        "rental_out_at" => null,
        "return_at" => null
    ];
});

/**
 * 创建的时候是要传入order_id字段
 */
$factory->define(OrderBundle::class, function (Faker $faker) {
    $bundle = factory(FilterableAssetBundle::class)->create();
    // need order id
    return [
        "name" => $faker->name,
        "bundle_id" => $bundle->id,
        "name" => $bundle->name,
        "amount" => $faker->numberBetween(1, 100),
        "rental_out_at" => null,
        "return_at" => null
    ];
});



$factory->define(Order::class, function (Faker $faker) {
    $date1 = $faker->dateTimeBetween('+1 week', '+1 month');
    $date2 = $faker->dateTimeBetween('+1 week', '+1 month');
    $date3 = $faker->dateTimeBetween('+1 week', '+1 month');
    $dates = [$date1, $date2, $date3];
    sort($dates);
    $statusId = OrderStatus::all()->pluck('id');
    $paymentMethodIds = PaymentMethod::all()->pluck('id');
    $amount1 = $faker->randomNumber(4);
    $amount2 = $faker->randomNumber(4);
    $isPaid = $faker->boolean;
    $paidAt = null;
    if ($isPaid) {
        $paidAt = $faker->dateTimeBetween('+1 week', '+1 month');
    }
    return [
        "order_number" => OrderNumberGenerator::id(),
        "customer_id" => factory(Customer::class)->create()->id,
        "description" => $faker->paragraph(3),
        "expected_rental_out_date" => $dates[0],
        "expected_return_date" => $dates[2],
        "expected_ship_date" => $dates[1],
        "status_id" => $faker->randomElement($statusId),
        "payment_method_id" => $faker->randomElement($paymentMethodIds),
        "is_paid" => $isPaid,
        "paid_at" => $paidAt,
        "net_amount" => max($amount1, $amount2),
        "paid_amount" => min($amount1, $amount2),
        "payable_amount" => max($amount1, $amount2) - min($amount1, $amount2),
        "ship_address" => $faker->address,
        "identification_number" => $faker->countryISOAlpha3,
        "location" => $faker->city,
        "discount" => $faker->numberBetween(0, 100) / 100,
        "created_by" => factory(User::class)->create()->id,
        "assigned_to" => factory(User::class)->create()->id
    ];
});
